using IrisGenerator.Common.Tools;
using IrisGenerator.Common.Core;
using System.ComponentModel;
using IrisGenerator.Framework.RuleEngine;
using IrisGenerator.Framework.Events;
namespace IrisGenerator.Common.DataTypes
{
    public partial class MetatypeEquivalences : BindableObject<MetatypeEquivalences>, IRuleable<MetatypeEquivalences>, INotifyPropertyChanged
    {

        private System.Nullable<System.Guid> _tarId;

        private System.Nullable<System.Guid> _metId;

        private string _metEquType;

        private System.Nullable<bool> _metEquSpecifyLength;

        private System.Nullable<bool> _metEquSpecifyPrecision;

        private Metatype _metatype;

        private Target _target;

        public virtual System.Nullable<System.Guid> TarId
        {
            get
            {
                return _tarId;
            }
            set
            {
                _tarId = value;
                this.OnPropertyChanged("TarId");
            }
        }

        public virtual System.Nullable<System.Guid> MetId
        {
            get
            {
                return _metId;
            }
            set
            {
                _metId = value;
                this.OnPropertyChanged("MetId");
            }
        }


        public virtual string MetEquType
        {
            get
            {
                return _metEquType;
            }
            set
            {
                _metEquType = value;
                this.OnPropertyChanged("MetEquType");
            }
        }

        public virtual System.Nullable<bool> MetEquSpecifyLength
        {
            get
            {
                return _metEquSpecifyLength;
            }
            set
            {
                _metEquSpecifyLength = value;
                this.OnPropertyChanged("MetEquSpecifyLength");
            }
        }

        public virtual System.Nullable<bool> MetEquSpecifyPrecision
        {
            get
            {
                return _metEquSpecifyPrecision;
            }
            set
            {
                _metEquSpecifyPrecision = value;
                this.OnPropertyChanged("MetEquSpecifyPrecision");
            }
        }

        public virtual Metatype Metatype
        {
            get
            {
                if ((this._metatype == null))
                {
                    this._metatype = Metatype.Load(this._metId);
                }
                return this._metatype;
            }
            set
            {
                _metatype = value;
                if (_metatype != null)
                    _metId = _metatype.MetId;
            }
        }

        public virtual Target Target
        {
            get
            {
                if ((this._target == null))
                {
                    this._target = Target.Load(this._tarId);
                }
                return this._target;
            }
            set
            {
                _target = value;
            }
        }

        private void Clean()
        {
            this.TarId = null;
            this.MetId = null;
            this.MetEquType = string.Empty;
            this.MetEquSpecifyLength = null;
            this.MetEquSpecifyPrecision = null;
            this.Metatype = null;
            this.Target = null;
        }

        private void Fill(System.Data.DataRow dr)
        {
            this.Clean();
            if ((dr["TAR_ID"] != System.DBNull.Value))
            {
                this.TarId = ((System.Nullable<System.Guid>)(dr["TAR_ID"]));
            }
            if ((dr["MET_ID"] != System.DBNull.Value))
            {
                this.MetId = ((System.Nullable<System.Guid>)(dr["MET_ID"]));
            }
            if ((dr["MET_EQU_TYPE"] != System.DBNull.Value))
            {
                this.MetEquType = ((string)(dr["MET_EQU_TYPE"]));
            }
            if ((dr["MET_EQU_SPECIFY_LENGTH"] != System.DBNull.Value))
            {
                this.MetEquSpecifyLength = ((System.Nullable<bool>)(dr["MET_EQU_SPECIFY_LENGTH"]));
            }
            if ((dr["MET_EQU_SPECIFY_PRECISION"] != System.DBNull.Value))
            {
                this.MetEquSpecifyPrecision = ((System.Nullable<bool>)(dr["MET_EQU_SPECIFY_PRECISION"]));
            }
        }

        public static MetatypeEquivalencesCollection Select_METATYPE_EQUIVALENCES_By_MET_ID(System.Nullable<System.Guid> MET_ID)
        {
            IrisGenerator.Dal.MetatypeEquivalences dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.MetatypeEquivalences();
                System.Data.DataSet ds = dbo.Select_METATYPE_EQUIVALENCES_By_MET_ID(MET_ID);
                MetatypeEquivalencesCollection collection = new MetatypeEquivalencesCollection();
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1))
                    {
                        MetatypeEquivalences obj = new MetatypeEquivalences();
                        obj.Fill(ds.Tables[0].Rows[i]);
                        if ((obj != null))
                        {
                            collection.Add(obj);
                        }
                    }
                }
                return collection;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public static MetatypeEquivalencesCollection Select_METATYPE_EQUIVALENCES_By_TAR_ID(System.Nullable<System.Guid> TAR_ID)
        {
            IrisGenerator.Dal.MetatypeEquivalences dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.MetatypeEquivalences();
                System.Data.DataSet ds = dbo.Select_METATYPE_EQUIVALENCES_By_TAR_ID(TAR_ID);
                MetatypeEquivalencesCollection collection = new MetatypeEquivalencesCollection();
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1))
                    {
                        MetatypeEquivalences obj = new MetatypeEquivalences();
                        obj.Fill(ds.Tables[0].Rows[i]);
                        if ((obj != null))
                        {
                            collection.Add(obj);
                        }
                    }
                }
                return collection;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public static MetatypeEquivalencesCollection GetAll()
        {
            IrisGenerator.Dal.MetatypeEquivalences dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.MetatypeEquivalences();
                System.Data.DataSet ds = dbo.METATYPE_EQUIVALENCES_Select_All();
                MetatypeEquivalencesCollection collection = new MetatypeEquivalencesCollection();
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1))
                    {
                        MetatypeEquivalences obj = new MetatypeEquivalences();
                        obj.Fill(ds.Tables[0].Rows[i]);
                        if ((obj != null))
                        {
                            collection.Add(obj);
                        }
                    }
                }
                return collection;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public static MetatypeEquivalences Load(System.Nullable<System.Guid> TAR_ID, System.Nullable<System.Guid> MET_ID)
        {
            IrisGenerator.Dal.MetatypeEquivalences dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.MetatypeEquivalences();
                System.Data.DataSet ds = dbo.METATYPE_EQUIVALENCES_Select_One(TAR_ID, MET_ID);
                MetatypeEquivalences obj = null;
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    if ((ds.Tables[0].Rows.Count > 0))
                    {
                        obj = new MetatypeEquivalences();
                        obj.Fill(ds.Tables[0].Rows[0]);
                    }
                }
                return obj;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public virtual void Load()
        {
            IrisGenerator.Dal.MetatypeEquivalences dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.MetatypeEquivalences();
                System.Data.DataSet ds = dbo.METATYPE_EQUIVALENCES_Select_One(this.TarId, this.MetId);
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    if ((ds.Tables[0].Rows.Count > 0))
                    {
                        this.Fill(ds.Tables[0].Rows[0]);
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public override void Insert()
        {
            IrisGenerator.Dal.MetatypeEquivalences dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.MetatypeEquivalences();
                dbo.METATYPE_EQUIVALENCES_Insert(this.TarId, this.MetId, this.MetEquType, this.MetEquSpecifyLength, this.MetEquSpecifyPrecision);
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public override bool Delete(bool fireRuleMessages)
        {
            RuleErrorCollection errors = null;
            if (this.ObjectRules.CanDelete(ref errors))
            {
                IrisGenerator.Dal.MetatypeEquivalences dbo = null;
                try
                {
                    dbo = new IrisGenerator.Dal.MetatypeEquivalences();
                    BaseEventArgs<MetatypeEquivalences> e = new BaseEventArgs<MetatypeEquivalences>(this, fireRuleMessages);
                    this.OnDeleting(e);
                    if (!e.Cancel)
                    {
                        dbo.METATYPE_EQUIVALENCES_Delete(this.TarId,this.MetId);
                        return true;
                    }
                    else
                        return false;
                }
                catch (System.Exception ex)
                {
                    throw;
                }
                finally
                {
                    if ((dbo != null))
                    {
                        dbo.Dispose();
                    }
                }
            }
            else
            {
                //TODO: Traitement des erreurs...
                return false;
            }
        }

        public override void Update()
        {
            IrisGenerator.Dal.MetatypeEquivalences dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.MetatypeEquivalences();
                dbo.METATYPE_EQUIVALENCES_Update(this.TarId, this.MetId, this.MetEquType, this.MetEquSpecifyLength, this.MetEquSpecifyPrecision);
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }
    }
}
